package com.lemon.funplus;

import java.util.ArrayList;

import android.accounts.AccountManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lemon.funplus.parse.CMD;
import com.lemon.funplus.parse.JsonParser;
import com.lemon.funplus.ulti.BaseEngine;

public class AccountActiveActivity extends BaseActivity {
	EditText edtActive, edtPhone;
	TextView textCaution;
	Button btnActive, btnGetcode;
	String activeCode = "", phone = "";
	AccountManager mAccountManager;
	AlertDialog.Builder alertDialog;
	Resources resources;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_active);
		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra("phone"))
			phone = extras.getString(phone);
		initView();
		edtPhone.setText(phone);
	}

	@Override
	protected void onResume() {
		super.onResume();
		resources = getResources();
		menuFragment.ACCOUNT.select();
		myActionBar.setTitle(resources.getString(R.string.active));
	}

	void initView() {
		edtPhone = (EditText) findViewById(R.id.active_edittext_phone);
		edtActive = (EditText) findViewById(R.id.active_edittext_activecode);
		textCaution = (TextView) findViewById(R.id.active_textview_caution);
		btnActive = (Button) findViewById(R.id.active_button_active);
		btnGetcode = (Button) findViewById(R.id.active_button_getactivecode);
		edtActive.setOnEditorActionListener(new EditText.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					activeAccount();
				}
				return false;
			}
		});
		btnActive.setOnClickListener(clickListener);
		btnGetcode.setOnClickListener(clickListener);
	}

	private void activeAccount() {
		if (!edtActive.getText().toString().equals("") && !edtPhone.getText().toString().equals("")) {
			phone=edtPhone.getText().toString();
			if (BaseEngine.checkVinaPhone(phone)) {
				textCaution.setText("");
				activeCode = edtActive.getText().toString();
				new AsyncTaskParseJson().execute("");
			} else
				textCaution.setText(resources.getString(R.string.phone_error));
		} else
			textCaution.setText("Hãy nhập đầy đủ thông tin");
	}

	private void getActiveCode() {
		alertDialog = new AlertDialog.Builder(AccountActiveActivity.this);
		alertDialog.setTitle("Lấy mã xác thực");
		final EditText input = new EditText(AccountActiveActivity.this);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		input.setLayoutParams(lp);
		input.setHint("Số điện thoại");
		alertDialog.setView(input);
		alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
		if (BaseEngine.checkVinaPhone(phone))
			input.setText(phone);
		alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if (BaseEngine.checkVinaPhone(input.getText().toString())) {
					new AsyncGetactiveCode().execute(input.getText().toString());
				} else
					Toast.makeText(AccountActiveActivity.this, resources.getString(R.string.phone_error), Toast.LENGTH_SHORT).show();
			}
		});

		alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		alertDialog.show();
	}

	OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.active_button_active:
				activeAccount();
				break;
			case R.id.active_button_getactivecode:
				getActiveCode();
				break;
			default:
				break;
			}
		}
	};

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		ProgressDialog progressDialog = new ProgressDialog(AccountActiveActivity.this);

		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.active(phone, activeCode);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			// BaseEngine.showLogformLongString("",result);
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null)
				try {
					ArrayList<String> list = JsonParser.readListdata(result);
					if (list != null && list.size() > 1) {
						Log.e("", "status: " + list.get(0));
						Log.e("", "mss: " + list.get(1));
						Toast.makeText(AccountActiveActivity.this, list.get(1), Toast.LENGTH_SHORT).show();
						if(list.get(0).equals(CMD.TAG_SUCCESS)){
							Intent intent = new Intent(AccountActiveActivity.this, AccountLoginActivity.class);
							startActivity(intent);
	//						overridePendingTransition(R.anim.main, android.R.anim.slide_out_right);
							finish();
						}
					} else
						Toast.makeText(AccountActiveActivity.this, resources.getString(R.string.request_error), Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}

	public class AsyncGetactiveCode extends AsyncTask<String, String, String> {
		ProgressDialog progressDialog = new ProgressDialog(AccountActiveActivity.this);

		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.getActive(arg0[0]);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null)
				try {
					ArrayList<String> list = JsonParser.readListdata(result);
					if (list != null && list.size() > 1) {
						Log.e("", "status: " + list.get(0));
						Log.e("", "mss: " + list.get(1));
						Toast.makeText(AccountActiveActivity.this, list.get(1), Toast.LENGTH_SHORT).show();
					} else
						Toast.makeText(AccountActiveActivity.this, resources.getString(R.string.request_error), Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
//	@Override
//	public void onBackPressed() {
//		super.onBackPressed();
//		overridePendingTransition(R.anim.main, android.R.anim.slide_out_right);
//	}
}
